package cn.iocoder.yudao.module.digital.controller.admin.cardkeythirdpartyapirecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.digital.controller.admin.cardkeythirdpartyapirecord.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.cardkeythirdpartyapirecord.CardkeyThirdPartyApiRecordDO;
import cn.iocoder.yudao.module.digital.service.cardkeythirdpartyapirecord.CardkeyThirdPartyApiRecordService;

@Tag(name = "管理后台 - 卡密第三方API请求记录")
@RestController
@RequestMapping("/digital/cardkey-third-party-api-record")
@Validated
public class CardkeyThirdPartyApiRecordController {

    @Resource
    private CardkeyThirdPartyApiRecordService cardkeyThirdPartyApiRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建卡密第三方API请求记录")
    @PreAuthorize("@ss.hasPermission('digital:cardkey-third-party-api-record:create')")
    public CommonResult<Long> createCardkeyThirdPartyApiRecord(@Valid @RequestBody CardkeyThirdPartyApiRecordSaveReqVO createReqVO) {
        return success(cardkeyThirdPartyApiRecordService.createCardkeyThirdPartyApiRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新卡密第三方API请求记录")
    @PreAuthorize("@ss.hasPermission('digital:cardkey-third-party-api-record:update')")
    public CommonResult<Boolean> updateCardkeyThirdPartyApiRecord(@Valid @RequestBody CardkeyThirdPartyApiRecordSaveReqVO updateReqVO) {
        cardkeyThirdPartyApiRecordService.updateCardkeyThirdPartyApiRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除卡密第三方API请求记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('digital:cardkey-third-party-api-record:delete')")
    public CommonResult<Boolean> deleteCardkeyThirdPartyApiRecord(@RequestParam("id") Long id) {
        cardkeyThirdPartyApiRecordService.deleteCardkeyThirdPartyApiRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得卡密第三方API请求记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('digital:cardkey-third-party-api-record:query')")
    public CommonResult<CardkeyThirdPartyApiRecordRespVO> getCardkeyThirdPartyApiRecord(@RequestParam("id") Long id) {
        CardkeyThirdPartyApiRecordDO cardkeyThirdPartyApiRecord = cardkeyThirdPartyApiRecordService.getCardkeyThirdPartyApiRecord(id);
        return success(BeanUtils.toBean(cardkeyThirdPartyApiRecord, CardkeyThirdPartyApiRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得卡密第三方API请求记录分页")
    @PreAuthorize("@ss.hasPermission('digital:cardkey-third-party-api-record:query')")
    public CommonResult<PageResult<CardkeyThirdPartyApiRecordRespVO>> getCardkeyThirdPartyApiRecordPage(@Valid CardkeyThirdPartyApiRecordPageReqVO pageReqVO) {
        PageResult<CardkeyThirdPartyApiRecordDO> pageResult = cardkeyThirdPartyApiRecordService.getCardkeyThirdPartyApiRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CardkeyThirdPartyApiRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出卡密第三方API请求记录 Excel")
    @PreAuthorize("@ss.hasPermission('digital:cardkey-third-party-api-record:export')")
    @OperateLog(type = EXPORT)
    public void exportCardkeyThirdPartyApiRecordExcel(@Valid CardkeyThirdPartyApiRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CardkeyThirdPartyApiRecordDO> list = cardkeyThirdPartyApiRecordService.getCardkeyThirdPartyApiRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "卡密第三方API请求记录.xls", "数据", CardkeyThirdPartyApiRecordRespVO.class,
                        BeanUtils.toBean(list, CardkeyThirdPartyApiRecordRespVO.class));
    }

}